import { cacheTools, toast } from 'nc-lightapp-front';
import { getQueryCondition } from 'src/hrpub/common/utils/utils';
import { addPkorgRefer } from '../../../public/functions/addCondition'
export default class initAction {
	constructor(comp) {
		this.comp = comp;
		if(comp.props.use){
            comp.props.use.form('psninfoappform','old_bd_psndoc','new_bd_psndoc');
            comp.props.use.editTable('psninfoapprovegrid');
            comp.props.use.search('search');
        }
	}
	// 获取模板数据
	getTemplate = () => {
		const { props, action } = this.comp;
		const { createUIDom, psninfoApprove, meta, button } = props;
		const { lang, tabIndex } = psninfoApprove;
		createUIDom({ pagecode: psninfoApprove.pagecode, appcode: psninfoApprove.appcode }, (data) => {
			let psninfoapprovegridDatas = data.template.psninfoapprovegrid.items,
				psninfoappformDatas = data.template.psninfoappform.items;
			psninfoapprovegridDatas.push({
				attrcode: 'optionDelete',
				itemtype: 'customer',
				hyperlinkflag: false,
				label: lang['gx6008-000046'], // 国际化 '操作'
				width: '180px',
				textAlign: 'center',
				visible: false,
				fixed: 'right',
				render: (text, record, index) => {
					return (
						<div>
							<a
								href="#"
								style={{ color: '#0073E1', cursor: 'pointer', marginRight: '12px' }}
								onClick={(ev) => {
									ev.preventDefault();
									this.delItem(record, index);
								}}
							>
								{lang['gx6008-000039']}
							</a>
						</div>
					);
				}
			});
			psninfoapprovegridDatas.forEach((item) => {
				if (item.attrcode === 'pk_psndoc.name') {
					item.renderStatus = 'browse';
					item.render = (value, record, index) => {
						const __text__ =
							record && record.values['pk_psndoc.name'] && record.values['pk_psndoc.name'].value
								? record.values['pk_psndoc.name'].value
								: '___';
						return (
							<a
								href="#"
								onClick={(ev) => {
									ev.preventDefault();
									setTimeout(() => {
										this.toCardView(record, index);
									}, 500);
								}}
							>
								{__text__}
							</a>
						);
					};
				}
				if (item.attrcode === 'checkstatus') {
					item.renderStatus = 'browse';
					item.render = (value, record, index) => {
						const __val__ =
							record && record.values['checkstatus'] && record.values['checkstatus'].value
								? record.values['checkstatus'].value
								: '0';
								
						let __text__ = lang['gx6008-000200'];
						if (__val__ === '1') {
							__text__ = lang['gx6008-000209'] + lang['gx6008-000205'];
						}
						if (__val__ === '2') {
							__text__ = lang['gx6008-000209'] + lang['gx6008-000206'];
						}
						return (
							<span>
								{__text__}
							</span>
						);
					};
				}
			});
			psninfoappformDatas.forEach((item) => {
				if (item.attrcode === 'checkstatus') {
					item.render = (value, record, index) => {
						const __val__ =
							record && record['checkstatus'] && record['checkstatus'].value
								? record['checkstatus'].value
								: '0';
						let __text__ = lang['gx6008-000200'];
						if (__val__ === '1') {
							__text__ = lang['gx6008-000209'] + lang['gx6008-000205'];
						}
						if (__val__ === '2') {
							__text__ = lang['gx6008-000209'] + lang['gx6008-000206'];
						}
						return __text__;
					};
				}
			});
			meta.setMeta(data && data.template ? data.template : {});
			button.setButtons(data && data.button ? data.button : {});
		});
	};
	// 获取多语
	getLanguage = async () => {
		const { MultiInit, dispatch } = this.comp.props;
		return new Promise((resolve) => {
			MultiInit.getMultiLang({
				moduleId: 'gx6008',
				domainName: 'hrtrn',
				callback: (json, status, init) => {
					dispatch({
						type: 'psninfoApprove/update',
						payload: {
							lang: json
						}
					});
					resolve();
				}
			});
		});
	};
	/* 设置按钮状态 */
	setButtonShow = () => {
		const { props } = this.comp;
		const { psninfoApprove, button } = props;

		if (psninfoApprove.tabIndex === '0') {
			button.setButtonVisible([ 'multi-approve' ], true);
			if (!psninfoApprove.selectedRowPk.length) {
				button.setButtonDisabled([ 'multi-approve', 'approve' ], true);
			} else {
				button.setButtonDisabled([ 'multi-approve', 'approve' ], false);
			}
		} else {
			button.setButtonVisible([ 'multi-approve' ], false);
			button.setButtonDisabled([ 'approve' ], true);
		}
	};
	// 初始化
	didMount = async () => {
		if (location.hostname.match(/localhost/) !== null) {
			location.hash = '/ifr?page=201909091110';
		}
		// 优先获取多语
		await this.getLanguage();
		// 其次模板
		await this.getTemplate();
		await this.getHeight();
		this.setButtonShow();
		this.subscribe();
	};
	didUpdate = () => {
		this.setButtonShow();
	};
	// 卸载
	willUnMount = () => {
		this.pubSub.unSubscribe('setTableData');
		// this.pubSub.unSubscribe('setFormData');
	};
	// 事件订阅部分
	subscribe = () => {
		const { comp: { action }, pubSub } = this;
		pubSub.subscribe('setTableData', (...args) => {
			this.setTableData(...args);
		});
	};
	/* 获取当前页面可见高度 */
	getHeight = () => {
		const { psninfoApprove } = this.comp.props;
		let wH = window.innerHeight,
			domHeight = 0,
			gap = 80;
		let hasPkOrg = JSON.stringify(psninfoApprove.hrorgobj) === '{}' ? false : true;
		if (hasPkOrg) {
			domHeight += document.querySelector('.row-header') && document.querySelector('.row-header').clientHeight;
			domHeight += document.querySelector('.u-tabs') && document.querySelector('.u-tabs').clientHeight;
			domHeight += document.querySelector('.ncc-hr-pagination-wrapper')
				? document.querySelector('.ncc-hr-pagination-wrapper').clientHeight
				: 32;
		} else {
			domHeight += document.querySelector('.row-header') && document.querySelector('.row-header').clientHeight;
			gap = 20;
		}

		return wH - domHeight - gap;
	};
	/* tab切换 */
	tabChange = (tabIndex) => {
		const { dispatch, meta } = this.comp.props;
		let pageInfo = {
			pageIndex: 1,
			pageSize: 10,
			total: 1,
			totalPage: 1
		};
		dispatch({
			type: 'psninfoApprove/update',
			payload: {
				tabIndex,
				pageInfo
			}
		});
		setTimeout(() => {
			this.setTableData();
		}, 100);
	};
	/* table行双击 */
	doubleClick = (record, index) => {
		this.toCardView(record, index);
	};
	/* 设置列表页面数据 */
	setTableData = () => {
		const { psninfoApprove, dispatch, editTable } = this.comp.props;
		const { pageInfo, searchData, tabIndex } = psninfoApprove;
		const { pageIndex, pageSize } = pageInfo;
		if (JSON.stringify(psninfoApprove.hrorgobj) === '{}') return;
		let _postData_ = {
			pk_org: psninfoApprove.hrorgobj.refpk,
			checkstatus: psninfoApprove.tabIndex,
			pageInfo: {
				pageIndex,
				pageSize
			},
			searchData
		};
		dispatch({
			type: 'psninfoApprove/getTableData',
			payload: {
				postData: Object.assign({}, _postData_, psninfoApprove.searchData)
			}
		}).then((res) => {
			let hasData = res.data ? (JSON.stringify(res.data) === '{}' ? false : true) : false;
			if (hasData) {
				let dt = res.data.psninfoapprovegrid;
				dispatch({
					type: 'psninfoApprove/update',
					payload: {
						pageInfo: dt.pageInfo,
						hasData
					}
				});
				!parseInt(tabIndex)
					? editTable.hideColByKey('psninfoapprovegrid', 'optionDelete')
					: editTable.showColByKey('psninfoapprovegrid', 'optionDelete');
				editTable.setTableData('psninfoapprovegrid', dt);
				cacheTools.set('allpks', dt.allpks);
			} else {
				dispatch({
					type: 'psninfoApprove/update',
					payload: {
						hasData
					}
				});
				editTable.setTableData('psninfoapprovegrid', { rows: [] });
				cacheTools.set('allpks', []);
			}
		});
	};
	/* 查询刷新数据 */
	toSearch = () => {
		const { dispatch } = this.comp.props;
		let searchData = getQueryCondition(this.comp.props, 'search');
		dispatch({
			type: 'psninfoApprove/update',
			payload: {
				searchData
			}
		});
		setTimeout(() => {
			this.setTableData();
		}, 100);
	};
	/* 查看详情 */
	toCardView = (record, index) => {
		const { props, action } = this.comp;
		const { dispatch } = props;
		dispatch({
			type: 'psninfoApprove/update',
			payload: {
				showMode: 'card-browse',
				selectedRowPk: [ record.values.pk_psninfoapprove.value ]
			}
		});
		setTimeout(() => {
			action.cardAction.setDetailData(record, index);
		}, 100);
	};
	/* 列表checkbox checked事件 */
	selectedChange = (props, moduleId, newVal, oldVal) => {
		const { editTable, dispatch } = props;
		let selectedRowPk = [],
			selectedRow = editTable.getCheckedRows(moduleId);
		if (selectedRow.length) {
			selectedRow.forEach((item) => {
				selectedRowPk.push(item.data.values.pk_psninfoapprove.value);
			});
		} else {
			selectedRowPk = [];
		}
		dispatch({
			type: 'psninfoApprove/update',
			payload: {
				selectedRowPk
			}
		});
		// setTimeout(() => {
		// 	this.setButtonShow();
		// }, 100);
	};
	/* 删除行数据 */
	delItem = (record, index) => {
		const { props } = this.comp;
		const { dispatch, psninfoApprove } = props;
		const { selectedRowPk, lang } = psninfoApprove;
		let pks = record ? record.values.pk_psninfoapprove.value : selectedRowPk.join();
		let params = {
			pk_org: psninfoApprove.hrorgobj.refpk,
			pks
		};
		dispatch({
			type: 'psninfoApprove/delItem',
			payload: {
				postData: params
			}
		}).then((res) => {
			toast({ color: 'success', content: lang['gx6008-000017'] });
			this.setTableData();
		});
	};
}
